package com.atguigu.rabbitmq.three;

import com.atguigu.rabbitmq.utils.RabbitMqutils;
import com.atguigu.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class Work04 {

    //队列的名称
    public static final String TASK_QUEUE_NAME="闫文超ack";

    //接受消息
    public static void main(String[] args)throws Exception {
        Channel channel = RabbitMqutils.getChannel();
        System.out.println("C2等待接受消息出来等待时间较长");

        DeliverCallback deliverCallback=(var1, var2)->{
            //沉睡1s
            SleepUtils.sleep(30);
            System.out.println("接受到的消息"+new String(var2.getBody()));


            //手动应答
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 fasle不批量应答信道中的消息 true批量
             */
            channel.basicAck(var2.getEnvelope().getDeliveryTag(),false);


        };

        CancelCallback cancelCallback= var1->{
            System.out.println(var1+"消费者取消的消费接口回调");
        };
        //不公平分发
      //  int prefectchCount=1;
        //预取值
        int prefectchCount=5;
        channel.basicQos(prefectchCount);
        //采用手动应答
        boolean autAck=false;//不是自动的
        channel.basicConsume(TASK_QUEUE_NAME,autAck,deliverCallback,cancelCallback);

    }

}
